@extends('backend.layouts.app')

@section('content')
<div class="layui-card">
    <div class="layui-card-header layui-card-header-auto">
        <div class="layui-row">
            <div class="layui-col-md9">&nbsp;</div>
            <div class="layui-col-md3">
                <div class="layui-btn-group" style="float:right">
                    <button class="layui-btn layui-btn-sm layui-btn-danger" id="listDelete">删除</button>
                    <a href="{{route('admin.auth.manager.create')}}" class="layui-btn layui-btn-sm">添加</a>
                </div>
            </div>
        </div>
    </div>
    <div class="layui-card-body">
        <table id="dataTable" lay-filter="dataTable"></table>
        <script type="text/html" id="options">
            <div class="layui-btn-group">
                <a class="layui-btn layui-btn-sm" lay-event="show">查看</a>
                <a class="layui-btn layui-btn-sm" lay-event="edit">编辑</a>
                <a class="layui-btn layui-btn-sm" lay-event="changePassword">修改密码</a>
                <a class="layui-btn layui-btn-sm" lay-event="initPassword">重置密码</a>
                <a class="layui-btn layui-btn-sm" lay-event='role'>角色</a>
                <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</a>
            </div>

        </script>
    </div>
</div>
@endsection

@section('script')
<script type="text/javascript">
    layui.use(['layer', 'table', 'form'], function () {
        let layer = layui.layer;
        let form = layui.form;
        let table = layui.table;

        let dataTable = table.render({
            elem: '#dataTable',
            url: "{{route('admin.auth.manager.list')}}",
            page: true,
            cols: [
                [{
                    checkbox: true,
                    fixed: true
                }, {
                    field: 'name',
                    width: 120,
                    title: '姓名'
                }, {
                    field: 'username',
                    width: 120,
                    title: '用户名'
                }, {
                    field: 'sex',
                    title: '性别',
                    width: 80,
                    templet: function (res) {
                        return res.sex == 1 ? '男' : '女';
                    }
                }, {
                    field: 'mobile',
                    title: '手机号',
                    width: 120
                }, {
                    field: 'status',
                    title: '状态',
                    width: 100,
                    templet: function (res) {
                        let temp = res.status == 1 ?
                            '<input type="checkbox" lay-skin="primary" title="有效" disabled checked>' :
                            '<input type="checkbox" lay-skin="primary" title="有效" disabled>';
                        return temp;
                    }
                }, {
                    field: 'description',
                    title: '备注'
                }, {
                    fixed: 'right',
                    align: 'center',
                    toolbar: '#options'
                }]
            ]
        });

        table.on('tool(dataTable)', function (obj) {
            let data = obj.data;
            let layEvent = obj.event;
            switch (layEvent) {
                case 'show':
                    location.href = 'manager/' + data.manager_id + '/show';
                    break;
                case 'edit':
                    location.href = 'manager/' + data.manager_id + '/edit';
                    break;
                case 'initPassword':
                    location.href = 'manager/' + data.manager_id + '/init/password';
                    break;
                case 'changePassword':
                    location.href = 'manager/' + data.manager_id + '/change/password';
                    break;
                case 'role':
                    location.href = 'manager/' + data.manager_id + '/role';
                    break;
                case 'del':
                    layer.confirm('确认删除吗？', function (index) {
                        $.post("{{ route('admin.auth.manager.destroy') }}", {
                                '_method': 'delete',
                                '_token': '{{csrf_token()}}',
                                ids: data.manager_id
                            },
                            function (result) {
                                if (result.code == 0) {
                                    obj.del(); //删除对应行（tr）的DOM结构
                                }
                                layer.close(index);
                                layer.msg(result.msg);
                            });
                    });
                    break;
            }
        })
    });

</script>
@endsection
